/*
 * Games.java
 *
 * Created on 21. Juni 2008, 08:40
 */

package ads.client;

import java.util.List;

import javax.swing.JOptionPane;

import ads.exception.PlayingGameException;
import ads.util.GameInfo;
import ads.util.PlayerInfo;

/**
 * 
 * @author xian
 */
public class GamesFrame extends javax.swing.JFrame {
	ClientImpl client;

	/** Creates new form Games */
	public GamesFrame(ClientImpl client) {
		this.client = client;
		this.setTitle(client.getPlayer().getName());
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jButtonJoin = new javax.swing.JButton();
		jButtonExit = new javax.swing.JButton();
		jButtonNew = new javax.swing.JButton();
		jButtonRefresh = new javax.swing.JButton();
		jSplitPane1 = new javax.swing.JSplitPane();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTablePlayers = new javax.swing.JTable();
		jScrollPane2 = new javax.swing.JScrollPane();
		jTableGames = new javax.swing.JTable();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setResizable(false);
		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent evt) {
				BeforWindowClose(evt);
			}
		});

		jButtonJoin.setText("Join");
		jButtonJoin.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonJoinActionPerformed(evt);
			}
		});

		jButtonExit.setText("Exit");
		jButtonExit.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonExitActionPerformed(evt);
			}
		});

		jButtonNew.setText("New");
		jButtonNew.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonNewActionPerformed(evt);
			}
		});

		jButtonRefresh.setText("refresh");
		jButtonRefresh.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonRefreshActionPerformed(evt);
			}
		});

		jSplitPane1.setDividerLocation(200);
		jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

		jTablePlayers.setModel(new ads.client.PlayerTableModel());
		jScrollPane1.setViewportView(jTablePlayers);

		jSplitPane1.setBottomComponent(jScrollPane1);

		jTableGames.setModel(new ads.client.GameTableModel());
		jTableGames.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jTableGamesMouseClicked(evt);
			}
		});
		jScrollPane2.setViewportView(jTableGames);

		jSplitPane1.setTopComponent(jScrollPane2);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addContainerGap().addComponent(
						jButtonJoin, javax.swing.GroupLayout.DEFAULT_SIZE, 85,
						Short.MAX_VALUE).addGap(18, 18, 18).addComponent(
						jButtonNew, javax.swing.GroupLayout.DEFAULT_SIZE, 85,
						Short.MAX_VALUE).addGap(18, 18, 18).addComponent(
						jButtonRefresh, javax.swing.GroupLayout.DEFAULT_SIZE,
						85, Short.MAX_VALUE).addGap(19, 19, 19).addComponent(
						jButtonExit, javax.swing.GroupLayout.DEFAULT_SIZE, 84,
						Short.MAX_VALUE).addGap(11, 11, 11)).addComponent(
				jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 415,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				javax.swing.GroupLayout.Alignment.TRAILING,
				layout.createSequentialGroup().addComponent(jSplitPane1,
						javax.swing.GroupLayout.DEFAULT_SIZE, 348,
						Short.MAX_VALUE).addGap(18, 18, 18).addGroup(
						layout.createParallelGroup(
								javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(jButtonExit).addComponent(
										jButtonJoin).addComponent(jButtonNew)
								.addComponent(jButtonRefresh))
						.addContainerGap()));

		pack();
	}// </editor-fold>

	private void BeforWindowClose(java.awt.event.WindowEvent evt) {
		// TODO add your handling code here:
	}

	private void jButtonRefreshActionPerformed(java.awt.event.ActionEvent evt) {
		List<GameInfo> games;
		try {
			games = client.getAllGame();
			((GameTableModel) jTableGames.getModel()).setData(games);
		} catch (PlayingGameException e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
		}

	}

	private void jButtonExitActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	private void jButtonNewActionPerformed(java.awt.event.ActionEvent evt) {
		Integer count = 0;
		String text = JOptionPane.showInputDialog(this,
				"pleas input the count of Player.2-4");
		try {
			count = Integer.valueOf(text);
		} catch (Exception e) {
		}
		if (count > 4 || count < 2) {
			JOptionPane.showInputDialog(this,
					" the count of Player should between 2 and 4");
			return;
		}
		try {
			client.createGame(count);
		} catch (PlayingGameException e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
		jButtonRefreshActionPerformed(null);
	}

	private void jButtonJoinActionPerformed(java.awt.event.ActionEvent evt) {
		int index = jTableGames.getSelectedRow();
		if (index < 0) {
			JOptionPane.showMessageDialog(this, "please select a game");
		} else {
			GameInfo game = ((GameTableModel) jTableGames.getModel()).getData()
					.get(index);

			try {
				GameJoinStatus status = client.joinGame(game);
				if (status == GameJoinStatus.GameFull)
					JOptionPane.showMessageDialog(this, "Game full");
				else if (status == GameJoinStatus.GameNotExist)
					JOptionPane.showMessageDialog(this, "Game does not exist");
			} catch (PlayingGameException e) {
				JOptionPane.showMessageDialog(this, e.getMessage());
			}
		}
	}

	private void jTableGamesMouseClicked(java.awt.event.MouseEvent evt) {
		int index = jTableGames.getSelectedRow();
		if (index < 0) {
			JOptionPane.showMessageDialog(this, "please select a game");
		}
		GameInfo game = ((GameTableModel) jTableGames.getModel()).getData()
				.get(index);
		List<PlayerInfo> players = game.getPlayers();
		((PlayerTableModel) jTablePlayers.getModel()).setData(players);
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		// java.awt.EventQueue.invokeLater(new Runnable() {
		// public void run() {
		// new GamesFrame().setVisible(true);
		// }
		// });
	}

	// Variables declaration - do not modify
	private javax.swing.JButton jButtonExit;
	private javax.swing.JButton jButtonJoin;
	private javax.swing.JButton jButtonNew;
	private javax.swing.JButton jButtonRefresh;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JSplitPane jSplitPane1;
	private javax.swing.JTable jTableGames;
	private javax.swing.JTable jTablePlayers;
	// End of variables declaration

}
